//index.js
//获取应用实例
var app = getApp();
Page({
  data: {
    window: {},
    page: 1,
    type: 1,
    laspage: 0,
    loading: 0,
    'text': '<view>你好</view>',
    listData: []
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.init(1);
  },

  
  onLoad: function (options) {
    var that = this;
    //console.log(options);
    var user_id = options.id ? options.id : 5;
    this.setData({ user_id: user_id });
    this.setData({ window: app.globalData.window })
   
    this.init();
  },
  getList() {
    if (this.data.loading == 1 || this.data.lastPage == 1) {
      return;
    }
    // this.setData({ page:0,});
    var user_id = this.data.user_id;
    var that = this;
    var page = that.data.page;
    //console.log(event_type);
    app.api("topicjoin/get", {
      page: page,
      cate_id: this.data.showOrder,
      //type: 'todolist',
      // pid2:0,
      limit: 30,
     // showType: 'my',
      status:3,
      order: 'SnsTopicJoin.lastupdate desc',
      user_id: user_id,
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      page++;
      if (res.data.list.length > 0) {
        that.setData({ listData: that.data.listData.concat(res.data.list) });
      } else {
        that.setData({ lastPage: 1 });
      }
      that.setData({ loading: 0, page: page });
      wx.stopPullDownRefresh();

    })
  },
  scrolltolower: function (e) {
    console.log(e);
    this.getList();
  },
  init:function(type){
    var that = this;
    if (type == 1) {
      this.setData({ page: 1, lastPage: 0, listData: [] });
    }
    
    this.getList();
    //用户信息 
    app.api("user/info", {
      user_id: this.data.user_id
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      //console.log(res);
      wx.setNavigationBarTitle({
        title: res.data.nickname
      });
      that.setData({ user_info: res.data });
      wx.stopPullDownRefresh();
    })   
  },
  //关注一个用户
  follow:function(){
    var that =this;
    if (this.data.loading == 1 || this.data.user_info.hasFollow==1){
      return false;
    }
    var user_info = this.data.user_info;
    this.data.loading =1
    app.api("friends/add",{
      method: 'POST',
      to_user_id: this.data.user_info.user_id
    }, function (res){
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
      }
      user_info.hasFollow=1;
      that.setData({
        user_info: user_info
      });
    })
  }
})
